#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
收银台 - 预下单功能使用示例
"""

import os
import sys

# 添加项目根目录到Python路径，确保能够导入jlpay包
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from examples.base_example import BaseExample
from jlpay.services.cashier_service import CashierService
from jlpay.model.cashier import PreOrderRequest


class PreOrderExample(BaseExample):
    """预下单示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.cashier.pre_order")
        # 创建收银台服务
        self.cashier_service = CashierService(self.config)
    
    def build_pre_order_request(self):
        """构建预下单请求"""
        request = PreOrderRequest()
        
        # 设置请求参数
        request.set_merch_no("849584358120018")
        request.set_term_no("60131234")
        request.set_out_trade_no("ld201911023465529")
        request.set_payment_valid_time("10")
        request.set_description("充值中心-会员充值")
        request.set_attach("商城—-易拉宝10个打印纸20箱")
        request.set_total_amount("1")
        request.set_remark("该订单物流请走顺丰快递")
        request.set_notify_url("https://help.jlpay.com/api/docs/home")
        request.set_env_version("release")
        request.set_merch_appid("wx336dcaf6a1ecf632")
        request.set_return_url("page/home/webview/webview?src=https%3A%2F%2Fmp.elong.com%2Ftenthousandaura%2F%3Factivitycode%3Dd32d7735-ae4f-440c-bb0c-dba4deba9470%26isSocket%3DHotelActivityId_8407&isRefresh=refresh&isSocket=HotelActivityId_8407**ecf632")
        
        return request
    
    def demo(self):
        """执行预下单示例"""
        self.logger.info("开始执行预下单示例")
        
        # 创建预下单请求
        request = self.build_pre_order_request()
        
        # 发送请求并处理响应
        response = self.send_request_and_handle_response(
            self.cashier_service.create_pre_order,
            "预下单",
            request
        )
        
        # 处理响应结果
        if response and response.is_success():
            self.logger.info(f"响应报文：{response.to_dict()}")
        
        self.logger.info("预下单示例执行完毕")


if __name__ == "__main__":
    logger = PreOrderExample().logger
    logger.info("==================================================")
    logger.info("===== 预下单示例 开始 =====")
    logger.info("==================================================")
    
    example = PreOrderExample()
    example.demo()
    
    logger.info("==================================================")
    logger.info("===== 预下单示例执行完毕 =====")
    logger.info("==================================================") 